package com.zy.mc_module.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.zy.mc_module.R;
import com.zy.mc_module.base.BaseActivity;
import com.zy.mc_module.common.RxBusCode;
import com.zy.mc_module.mvp.bean.UserBean;
import com.zy.mc_module.mvp.contract.LoginContract;
import com.zy.mc_module.mvp.presenter.LoginPresenter;
import com.zy.mc_module.ui.setting.setpay.SetPayActivity;
import com.zy.mc_module.utils.*;
import com.zy.mc_module.view.ClearEditText;
import com.zy.mc_module.view.CountDownTextView;
import com.zy.mc_module.view.CustomHeadLayout;
import com.zy.mc_module.utils.retrofit.error.ThdException;

import butterknife.BindView;

/**
 * author : dq
 * date   : 19/5/10
 * desc   : 短信验证---修改支付密码/添加银行卡
 */
public class SmsActivity extends BaseActivity<LoginContract.View, LoginPresenter> implements
        LoginContract.View {

    @BindView(R.id.customhead)
    CustomHeadLayout customhead;
    @BindView(R.id.sms_tel)
    TextView tv_tel;
    @BindView(R.id.sms_getcode)
    CountDownTextView tv_getcode;
    @BindView(R.id.sms_code)
    ClearEditText et_code;

    private String code, title, tel, name, openingBank, bankId, bankNo;

    @Override
    protected boolean isNeedRxBus() {
        return true;
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sms;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        StatusBarUtil.setImmersiveStatusBar(this);
        title = getIntent().getStringExtra("title");
        customhead.setText(title);
        if ("短信验证".equals(title)) {
            //找回支付密码
            presenter.getCode(context, "10", UserUtils.getTel());
        } else if ("添加银行卡".equals(title)) {
            tel = getIntent().getStringExtra("tel");
            name = getIntent().getStringExtra("name");
            openingBank = getIntent().getStringExtra("openingBank");
            bankId = getIntent().getStringExtra("bankId");
            bankNo = getIntent().getStringExtra("bankNo");
            presenter.getCode(context, "14", tel);
        }
    }

    @Override
    protected void initListener() {

        customhead.setLeftClickListener(view -> finish());

        //输入框监听
        et_code.addMyTextChangedListener(s -> {
            if (null != s && s.length() == 6) {
                //自动校验验证码
                code = s.toString();
                if ("短信验证".equals(title)) {
                    presenter.checkCode(context, UserUtils.getTel(), code);
                } else if ("添加银行卡".equals(title)) {
                    presenter.checkCode(context, tel, code);
                }
            }
        });
    }

    /***
     * 获取验证码成功
     */
    @Override
    public void onGetCodeSuccess(String success) {
        if ("短信验证".equals(title)) {
            tv_tel.setText("验证码已发送至 " + StringsUtils.hideTel(UserUtils.getTel()));
        } else if ("添加银行卡".equals(title)) {
            tv_tel.setText("验证码已发送至 " + StringsUtils.hideTel(tel));
        }
        tv_getcode.setStart(context, 60000, 1000, new CountDownTextView.OnCountDownListener() {
            @Override
            public void onTick(long millisUntilFinished) {
                tv_getcode.setClickable(false);
                tv_getcode.setText(millisUntilFinished / 1000 + " s");
            }

            @Override
            public void onFinish() {
                tv_getcode.setClickable(true);
                tv_getcode.setText("获取验证码");
            }
        });
        tv_getcode.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if ("短信验证".equals(title)) {
                    presenter.getCode(context, "10", UserUtils.getTel());
                } else if ("添加银行卡".equals(title)) {
                    presenter.getCode(context, "11", tel);
                }
            }
        });
    }

    /***
     * 校验验证码成功
     */
    @Override
    public void onSuccess(boolean isSuc) {
        if (isSuc) {
            if ("短信验证".equals(title)) {
                Intent intent = new Intent(context, SetPayActivity.class);
                intent.putExtra("title", "设置支付密码");
                intent.putExtra("code", code);
                startActivity(intent);
            } else if ("添加银行卡".equals(title)) {
                presenter.bindBank(context, name, openingBank, tel, code, bankId, bankNo);
            }
        } else {
            ToastUtil.showShort("验证码错误");
        }
    }

    @Override
    public <T> void onSuccess(T t, int type) {

    }

    @Override
    public void onLoginSuccess(UserBean userBean) {

    }

    @Override
    public void onError(ThdException error) {
        ToastUtil.showShort(error.message);
    }

    @Subscribe(thread = EventThread.MAIN_THREAD, tags = {@Tag(RxBusCode.DIS_CODE)})
    public void dis(String param) {
        if (!isFinishing()) {
            finish();
        }
    }
}
